package com.yuan.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuan.blog.entity.SystemUserEntity;
import com.yuan.blog.mapper.SystemUserMapper;
import com.yuan.blog.service.ISystemUserService;
import com.yuan.blog.vo.MyPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
@Slf4j
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper,SystemUserEntity> implements ISystemUserService {



    @Override
    public void systemUserPage(Page<SystemUserEntity> page,Integer curPage) {
        page.setCurrent(curPage);
        QueryWrapper<SystemUserEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        wrapper.eq("status",0);
        this.page(page,wrapper);
        log.warn("总页数: " + page.getPages());
        log.warn("总条数: " + page.getTotal());
        log.warn("当前页: " + page.getCurrent());
        log.warn("每页显示条数: " + page.getSize());
        log.warn("当前页的数据:\n");
        for (SystemUserEntity record : page.getRecords()) {
            log.warn(record.toString() + "\n");
        }
    }

    /**
    * @Description: 查找所有状态正常的用户
    * @Param: 
    * @return: 
    * @Author: yuanboss
    * @Datetime: 2022/12/18 11:41 
    */
    @Override
    public List<SystemUserEntity> list() {
        QueryWrapper<SystemUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        return super.list(wrapper);
    }


    /**
    * @Description: 通过id进行逻辑删除
    * @Param: 
    * @return: 
    * @Author: yuanboss
    * @Datetime: 2022/12/18 14:22 
    */
    @Override
    public boolean removeById(Serializable id) {
        UpdateWrapper<SystemUserEntity> wrapper = new UpdateWrapper<SystemUserEntity>();
        wrapper.eq("id",id).eq("status",0);
        SystemUserEntity systemUser = new SystemUserEntity();
        systemUser.setStatus(1);
        return update(systemUser,wrapper);
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        List<SystemUserEntity> systemUserEntityList = new ArrayList<>();
        for (Object o : list) {
            SystemUserEntity systemUser = new SystemUserEntity();
            systemUser.setStatus(1);
            systemUser.setId((Integer) o);
            systemUserEntityList.add(systemUser);
        }
        return updateBatchById(systemUserEntityList);
    }


}
